package com.spring.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.ReturnAccountInfo;
import com.spring.tool.App;
import com.spring.util.Constant;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * 查询账号信息
 * 
 * @author Administrator
 * 
 */
public class QueryAccountActivity extends BaseActivity {
	RestClient restClient = null;
	private TextView txaccounttype,txname,txUserNo, txUserName, txCardID,txOpenCardDate, txDeptName,
			txCardStatusName,txCardStartDate,txCardEndDate;
	MyHandler myHandler;
	ReturnAccountInfo accountinfo = null;
	String username = null;
	String logintype = null;
	String location_textStr;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queryaccount);
		// Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
		// startActivity(intent);
		txaccounttype =(TextView)findViewById(R.id.txaccounttype);
		txname =(TextView)findViewById(R.id.txname);
		txUserNo = (TextView) findViewById(R.id.txUserNo);
		txUserName = (TextView) findViewById(R.id.txUserName);
		txOpenCardDate = (TextView) findViewById(R.id.CardStartDate);
		txDeptName = (TextView) findViewById(R.id.txDeptName);
		txCardStatusName = (TextView) findViewById(R.id.txCardStatusName);
		txCardID =(TextView)findViewById(R.id.txCardID);
		txCardStartDate =(TextView)findViewById(R.id.txCardStartDate);
		txCardEndDate =(TextView)findViewById(R.id.txCardEndDate);
		SharedPreferences userInfo = getSharedPreferences("user_info", 0);
		username = userInfo.getString("username", "");
		logintype = userInfo.getString("logintype", "");
		if(logintype!=null){
			if(logintype.equals("1")){
				location_textStr="身份证号";
			}else if(logintype.equals("2")){
				location_textStr="学号";
			}else if(logintype.equals("3")){
				location_textStr="邮箱地址";
			}
		}
		ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView textView = (TextView) findViewById(R.id.title_tv);
		textView.setText("我的账户");
		myHandler = new MyHandler();
		restClient = new RestClient(QueryAccountActivity.this);
		String url = App.getInstance().url+"/QueryAccountInfo";
		boolean islianwang =new Constant().isConnect(QueryAccountActivity.this);
		if(islianwang){
			sendHttpDate(url);
		}
	}

	/**
	 * 接受消息,处理消息 ,此Handler会与当前主线程一块运行
	 * */

	class MyHandler extends Handler {
		public MyHandler() {
		}

		public MyHandler(Looper L) {
			super(L);
		}

		// 子类必须重写此方法,接受数据
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Log.d("MyHandler", "handleMessage......");
			super.handleMessage(msg);
			// 此处可以更新UI
			switch (msg.arg1) {
			case 1:
				if (accountinfo != null) {
					txaccounttype.setText(location_textStr);
					txname.setText(username);
					txUserNo.setText(accountinfo.getUserNo());
					txUserName.setText(accountinfo.getUserName());
					txCardID.setText(""+accountinfo.getCardID());
					txOpenCardDate.setText(accountinfo.getOpenCardDate());
					txDeptName.setText(accountinfo.getDeptName());
					txCardStatusName.setText(accountinfo.getCardStatusName());
					txCardStartDate.setText(accountinfo.getCardStartDate());
					txCardEndDate.setText(accountinfo.getCardEndDate());
					
				}else{
					Toast.makeText(QueryAccountActivity.this, "没有查询到相关数据",  Toast.LENGTH_SHORT).show();
				}

				break;
			case 2:
				break;
			default:
				break;
			}

		}
	}

	private void sendHttpDate(String url) {
		RequestParams requestparams = new RequestParams(url, "POST");
		requestparams.put("LoginAccount", username);
		requestparams.put("LoginType", logintype);
		TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				try {
					AnalysisData(responseString);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);

			}
		};
		try {
			restClient.post(url, requestparams, textHttpResponseHandler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * json数据解析
	 * 
	 * @param strResult
	 * @throws JSONException
	 */
	private void AnalysisData(String strResult) throws JSONException {
		/**
		 * 这里需要分析服务器回传的json格式数据，
		 */
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject jsonObject = null;
			try {
				jsonObject = (JSONObject) jsonParser.nextValue();
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			accountinfo = new ReturnAccountInfo();
			accountinfo.setCardID(jsonObject.getInt("CardID"));
			accountinfo.setOpenCardDate(jsonObject.getString("OpenCardDate"));
			accountinfo.setUserNo(jsonObject.getString("UserNo"));
			accountinfo.setUserName(jsonObject.getString("UserName"));
			accountinfo.setDeptName(jsonObject.getString("DeptName"));
			accountinfo.setCardStatusName(jsonObject
					.getString("CardStatusName"));
			accountinfo.setCardStartDate(jsonObject.getString("CardStartDate"));
			accountinfo.setCardEndDate(jsonObject.getString("CardEndDate"));
			accountinfo.setReservedField1(jsonObject
					.getString("ReservedField1"));

			accountinfo.setReservedField2(jsonObject
					.getString("ReservedField2"));
			Message msg = new Message();
			msg.arg1 = 1;
			QueryAccountActivity.this.myHandler.sendMessage(msg); // 向Handler发送消息,更新UI
			// accountinfo.setreturnMsg(jsonObject.getString("returnMsg"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
